package com.tedu.jsd2407_ivos_fzj.vehicle.service.impl;

import com.tedu.jsd2407_ivos_fzj.vehicle.mapper.VehicleMapper;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.dto.VehicleQuery;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.dto.VehicleSaveParam;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.entity.Vehicle;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.vo.VehicleVO;
import com.tedu.jsd2407_ivos_fzj.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    public List<VehicleVO> select(VehicleQuery vehicleQuery){
        log.debug("车辆查询业务，参数：{}",vehicleQuery);
        List<VehicleVO> vehicleVOList = vehicleMapper.select(vehicleQuery);
        return vehicleVOList;

    }

    @Override
    public void save(VehicleSaveParam vehicleSaveParam) {
        log.debug("车辆新增和修改业务，参数：{}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if(vehicleSaveParam.getId()==null){
            vehicle.setStatus("1");//设置状态为空闲
            vehicle.setGeofenceBindStatus("0");//设置围栏绑定状态为空闲

            vehicle.setCreateTime(new Date());
            vehicleMapper.insert(vehicle);
        }else {
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("删除业务，参数：{}",id);
        vehicleMapper.deleteById(id);
    }

    @Override
    public void removeGeofenceBindStatus(Long id) {
        log.debug("移除车辆围栏绑定状态，参数：{}",id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());


        vehicleMapper.removeGeofenceBindStatus(vehicle);
    }

    @Override
    public void bindGeofence(Long id, Long geofenceId) {
        log.debug("绑定围栏业务，参数：{},{}",id,geofenceId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
